﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SouthApps.MobileHealth.SDK.DAL.DataAccess;
using SouthApps.MobileHealth.SDK.Common.Exceptions;
using SouthApps.MobileHealth.SDK.DAL.Model;
using SouthApps.MobileHealth.SDK.Common;

namespace SouthApps.MobileTasks.SDK.DAL.Repositories
{
    public class AdministrationRepository : IAdministrationRepository
    {
        PlatformDao platformDao;
        ServerDao serverDao;
        PlatformLicenseDao platformLicenseDao;

        public AdministrationRepository()
        {
            this.platformDao = new PlatformDao();
            this.serverDao = new ServerDao();
            this.platformLicenseDao = new PlatformLicenseDao();
        }

        public void AddPlatform(Platform platform)
        {
            try
            {
                this.platformDao.AddPlatform(platform);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while adding a platform.", ex.Message);
            }
        }

        public void AddPlatformLicense(PlatformLicense platformLicense)
        {
            try
            {
                this.platformLicenseDao.AddPlatformLicense(platformLicense);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while adding a licencia platform.", ex.Message);
            }
        }

        public List<Platform> GetPlatforms()
        {
            try
            {
                return this.platformDao.GetPlatforms();
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while getting platforms.", ex.Message);
            }
        }

        public Platform GetPlatform(string platformName)
        {
            try
            {
                return this.platformDao.GetPlatformByName(platformName);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while getting platform.", ex.Message);
            }
        }

        public PlatformLicense GetEntityPlatformLicense(int licensedEntityId, int platformId)
        {
            try
            {
                return this.platformLicenseDao.GetEntityPlatformLicense(licensedEntityId, platformId);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while getting an entity platform license.", ex.Message);
            }
        }

        public List<PlatformLicense> GetEntityPlatformLicenses(int licensedEntityId)
        {
            try
            {
                return this.platformLicenseDao.GetEntityPlatformLicenses(licensedEntityId);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while getting entity platform licenses.", ex.Message);
            }
        }

        public void ModifyPlatform(Platform platform)
        {
            try
            {
                this.platformDao.ModifyPlatform(platform);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while modifying a platform.", ex.Message);
            }
        }

        public void ModifyPlatformLicense(PlatformLicense platformLicense)
        {
            try
            {
                this.platformLicenseDao.ModifyPlatformLicense(platformLicense);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while modifying a platform license.", ex.Message);
            }
        }

        public void AddServer(Server server)
        {
            try
            {
                this.serverDao.AddServer(server);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while adding a server.", ex.Message);
            }
        }

        public List<Server> GetServers()
        {
            try
            {
                return this.serverDao.GetServers();
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while getting servers.", ex.Message);
            }
        }

        public void ModifyServer(Server server)
        {
            try
            {
                this.serverDao.ModifyServer(server);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while modifying a server.", ex.Message);
            }
        }


        public Platform GetPlatformByName(string platformName)
        {
            try
            {
                return this.platformDao.GetPlatformByName(platformName);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "An error has ocurred while getting platform by name.", ex.Message);
            }
        }
    }
}
